package com.nefu.softlab.xjwc.utils;

import com.nefu.softlab.xjwc.model.Activity;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author LiXiwen
 * @date 2020/2/8 12:17
 */
public class TimeUtil {

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.UK);
    private static final DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter dt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    /**
     * UTC 通用标准时间转化, 线程安全
     *
     * 2020-03-31T16:00:00.000Z
     * @param oldDate
     */
    public static String getTime(String oldDate) {
        LocalDateTime date= LocalDateTime.parse(oldDate, dtf);
        return date.format(dt);
    }

    public static String getNowTime() {
        LocalDateTime date = LocalDateTime.now();
        return date.format(dt);
    }

    public static boolean isTime(Activity activity) {
        long now = LocalDate.now().atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        long before = LocalDate.parse(activity.getStartTime(), dtf).atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        long after = LocalDate.parse(activity.getEndTime(), dtf).atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        System.out.println(before + " " + now + " " + after);
        if (now >= before && now <= after) {
            return true;
        }
        return false;
    }


}
